package com.voltmao.webpro.utils.wechat.utils.dto.msg;

import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;
import com.google.gson.internal.LinkedTreeMap;
import com.voltmao.webpro.utils.DateUtil;
import com.voltmao.webpro.utils.wechat.WechatUtils;

import java.util.Date;

public class TemplateMsg {

    public TemplateMsg() {
        this.data.put("first", null);
        this.data.put("remark", null);
    }

    public String getTemplateId() {
        return templateId;
    }

    public void setTemplateId(String templateId) {
        this.templateId = templateId;
    }

    public String getOpenid() {
        return touser;
    }

    public void setOpenid(String openid) {
        this.touser = openid;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    private String touser;
    @SerializedName("template_id")
    private String templateId;
    private String url;

    /**
     * 设置标题
     *
     * @param value
     * @param color
     */
    public void setFirst(String value, String color) {
        this.data.put("first", new TempleteMsgText(value, color));
    }

    /**
     * 设置内容描述
     *
     * @param value
     * @param color
     */
    public void setRemark(String value, String color) {
        this.data.put("remark", new TempleteMsgText(value, color));
    }

    /**
     * 增加内容
     *
     * @param value
     * @param color
     */
    public void addContent(String value, String color) {

        int index = (this.data.size() - 2) + 1;

        this.data.put("keynote" + index, new TempleteMsgText(value, color));
    }


    /**
     * 增加内容
     *
     * @param value
     * @param color
     */
    public void addContent(String value, String color, String keyName) {

        int index = (this.data.size() - 2) + 1;

        this.data.put(keyName + index, new TempleteMsgText(value, color));
    }


    /**
     * 设置标题信息
     *
     * @param first
     */
    public void setFirst(TempleteMsgText first) {
        this.data.put("first", first);
    }

    /**
     * 设置描述信息
     *
     * @param remark
     */
    public void setRemark(TempleteMsgText remark) {
        this.data.put("remark", remark);
    }

    private LinkedTreeMap<String, TempleteMsgText> data = new LinkedTreeMap<String, TempleteMsgText>();


    public String asJson() {
        TempleteMsgText remark = this.data.get("remark");
        this.data.remove("remark");
        this.data.put("remark", remark);


        return new GsonBuilder().create().toJson(this);
    }


    public static void main(String[] args) {

        final WechatUtils wechatUtils = WechatUtils.getInstance("usercar");
        final String openid = "ssss";
        TemplateMsg msg = new TemplateMsg();
        msg.setOpenid(openid);
        msg.setTemplateId(wechatUtils.getWxConfig().get("wx.templateSyId"));
        msg.setUrl("");
        msg.setRemark("广告收益", "#173177");
        msg.setFirst("路客士提醒您获得11元收益! ", "#173177");
        msg.addContent(DateUtil.getDateStr(new Date(), "yyyy年MM月dd日"), "#173177");


        System.out.println(msg.asJson());
    }


    public class TempleteMsgText {
        public String getColor() {
            return color;
        }

        public void setColor(String color) {
            this.color = color;
        }

        public String getValue() {
            return value;
        }

        public void setValue(String value) {
            this.value = value;
        }

        public TempleteMsgText(String value, String color) {
            this.color = color;
            this.value = value;
        }

        private String value;
        private String color;
    }

}


